﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using VswService_V7.Utility;

namespace VswService_V7.DataBase.Tables.L2INTERFACE
{
    public class L2INTF_EVENT
    {
        public L2INTF_EVENT()
        {

        }
        public L2INTF_EVENT(DataTable infcEventTable)
        {
            InfcEventTable = infcEventTable;
        }
        #region public field

        public static readonly string tableName = "L2INTF_EVENT";
        public static readonly string fd_eventNo = "EVENT_NO";
        public static readonly string fd_eventCode = "EVENT_CODE";
        public static readonly string fd_eventDate = "EVENT_DATE";
        public static readonly string fd_eventStatus = "EVENT_STATUS";
        public static readonly string fd_operator = "OPERATOR";
        public static readonly string fd_errorInfo = "ERROR_INFO";

        public const string insertNewTask = "2001";
        public const string deleteTask = "2002";
        public const string editTask = "2003";
        public const string insertNewStrip = "2004";
        public const string deleteStrip = "2005";
        //public const string editStrip = "2011";//文档缺少此eventCode
        public const string updateStripStatus = "2006";
        public const string generateOrUpdateDowntime = "2007";
        public const string generateOrUpdatePipepassport = "2008";
        public const string generateOrUpdateBundletag = "2009";
        public const string generateOrUpdateSamplepassport = "2010";
        public const string requestBatchNo = "2011";

        public DataTable InfcEventTable
        {
            get;
            set;
        }




        /// <summary>
		/// get eventid and eventcode when eventstate=-1
		/// </summary>
		/// <returns></returns>
		public static Dictionary<int, L2INFT_EVENT_RESULT> MonitorL2Infc_Event()
        {
            try
            {
                Dictionary<int, L2INFT_EVENT_RESULT> dicL2Event = new Dictionary<int, L2INFT_EVENT_RESULT>();
                string sql = string.Format("select * from {0} where {1}='-1'", tableName, fd_eventStatus);
                DataTable dataTable = OracleHelper.Exct_QueryTable(sql);
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    dicL2Event.Clear();
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        L2INFT_EVENT_RESULT result = new L2INFT_EVENT_RESULT();
                        result.eventCode = dr[fd_eventCode].ToString();
                        result.eventStatus = dr[fd_eventStatus].ToString();
                        //dicL2Event.Add(int.Parse(dr[fd_eventNo].ToString()), result);
                        if(!dicL2Event.ContainsKey(int.Parse(dr[fd_eventNo].ToString())))
                        {
                            dicL2Event.Add(int.Parse(dr[fd_eventNo].ToString()), result);
                        }
                        
                    }
                    return dicL2Event;
                }
                else
                {
                    return null;
                }
            }
            catch(Exception ex)
            {
                LogHelper.WriteLog("MonitorL2Infc_Event",ex);
                return null;
            }
        }



        public static void CloseEvent(int eventNo,string status)
        {
            string sqlCloseEvent = string.Format("update {0} set {1}='{2}' where {3}={4}",
                    tableName, fd_eventStatus, status, fd_eventNo, eventNo);
            OracleHelper.Exct_NonQuery(sqlCloseEvent);
        }

        #endregion
    }
}
